package cn.fresh.xuanyuan.module.product.controller.admin.orchard;

import cn.fresh.xuanyuan.framework.common.exception.ErrorCode;
import cn.fresh.xuanyuan.framework.common.pojo.CommonResult;
import cn.fresh.xuanyuan.framework.common.pojo.PageParam;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.module.product.dal.dataobject.orchard.OrchardDO;
import cn.fresh.xuanyuan.module.product.service.orchard.OrchardService;
import cn.fresh.xuanyuan.module.product.vo.orchard.OrchardRespVO;
import cn.fresh.xuanyuan.module.product.vo.orchard.OrchardSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "管理后台 - 果园")
@RestController
@RequestMapping("/product/orchard")
@Validated
public class OrchardController {

    @Resource
    private OrchardService orchardService;

    @PostMapping("/addOrchard")
    @Operation(summary = "添加合作果园")
    @PreAuthorize("@ss.hasPermission('product:orchard:add')")
    public CommonResult<String> addOrchard(@RequestBody @Validated OrchardSaveReqVO saveReqVO) {
        orchardService.save(saveReqVO);
        return CommonResult.success("添加成功");
    }

    @PostMapping("/editOrchard")
    @Operation(summary = "编辑果园")
    @PreAuthorize("@ss.hasPermission('product:orchard:edit')")
    public CommonResult<String> editOrchard(@RequestBody @Validated OrchardSaveReqVO saveReqVO) {
        if(saveReqVO.getId() == null) {
            return CommonResult.error(new ErrorCode(400, "id不能为空"));
        }
        orchardService.edit(saveReqVO);
        return CommonResult.success("编辑成功");
    }

    @GetMapping("/getAll")
    @Operation(summary = "获取果园列表")
    @PreAuthorize("@ss.hasPermission('product:orchard:getAll')")
    public CommonResult<List<OrchardRespVO>> getAll() {
        return CommonResult.success(orchardService.getAll());
    }
    @GetMapping("/getOrchardPage")
    @Operation(summary = "获取果园分页")
    @PreAuthorize("@ss.hasPermission('product:orchard:getOrchardPage')")
    public CommonResult<PageResult<OrchardDO>> getOrchardPage(@RequestParam(value = "name", required = false) String name,
                                                              @RequestParam(value = "phone", required = false) String phone, @ModelAttribute PageParam pageReq) {
        return CommonResult.success(orchardService.getOrchardPage(name, phone,pageReq));
    }

    @GetMapping("/getDetailById")
    @Operation(summary = "获取果园详情")
    public CommonResult<OrchardRespVO> getDetailById(@RequestParam("id") Integer id) {
        return CommonResult.success(orchardService.getDetailById(id));
    }
}
